package com.titleevaluation.controller;

import com.titleevaluation.entity.EducationInfo;
import com.titleevaluation.service.EducationInfoService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/applications/{applicationId}/education-info")
public class EducationInfoController {

    private final EducationInfoService educationInfoService;

    public EducationInfoController(EducationInfoService educationInfoService) {
        this.educationInfoService = educationInfoService;
    }

    @PostMapping
    public ResponseEntity<EducationInfo> createEducationInfo(@PathVariable Long applicationId, @RequestBody EducationInfo educationInfo) {
        EducationInfo createdEducationInfo = educationInfoService.createEducationInfo(applicationId, educationInfo);
        return ResponseEntity.ok(createdEducationInfo);
    }

    @GetMapping
    public ResponseEntity<List<EducationInfo>> getEducationInfoByApplicationId(@PathVariable Long applicationId) {
        List<EducationInfo> educationInfo = educationInfoService.getEducationInfoByApplicationId(applicationId);
        return ResponseEntity.ok(educationInfo);
    }

    @GetMapping("/{id}")
    public ResponseEntity<EducationInfo> getEducationInfoById(@PathVariable Long id) {
        return educationInfoService.getEducationInfoById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PutMapping("/{id}")
    public ResponseEntity<EducationInfo> updateEducationInfo(@PathVariable Long id, @RequestBody EducationInfo educationInfo) {
        EducationInfo updatedEducationInfo = educationInfoService.updateEducationInfo(id, educationInfo);
        return ResponseEntity.ok(updatedEducationInfo);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteEducationInfo(@PathVariable Long id) {
        educationInfoService.deleteEducationInfo(id);
        return ResponseEntity.noContent().build();
    }
}

